﻿Public Class Ntg

    Private Sub Ntg_Load(ByVal sEnder As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TopMost = True
    End Sub
    Dim data As String
    Dim fileo
    Dim files
    Dim p
    Private Sub Button1_Click(ByVal sEnder As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Button1.Enabled = False
        GroupBox1.Enabled = False
        GroupBox2.Enabled = False
        ListBox1.Items.Clear()
        ListBox1.Items.Add("<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & "UTF-8" & Chr(34) & " ?>")
        ListBox1.Items.Add("<gpx")
        ListBox1.Items.Add("version=" & Chr(34) & "1.0" & Chr(34) & "")
        ListBox1.Items.Add("creator=" & Chr(34) & "GPS Status - Freeware" & Chr(34) & "")
        ListBox1.Items.Add("xmlns:xsi=" & Chr(34) & "http://www.w3.org/2001/XMLSchema-instance" & Chr(34) & "")
        ListBox1.Items.Add("xmlns=" & Chr(34) & "http://www.topografix.com/GPX/1/0" & Chr(34) & "")
        ListBox1.Items.Add("xsi:schemaLocation=" & Chr(34) & "http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd" & Chr(34) & ">")
        ListBox1.Items.Add("<time>" & My.Computer.Clock.LocalTime.Year & "-" & My.Computer.Clock.LocalTime.Month & "-" & My.Computer.Clock.LocalTime.Day & "T" & My.Computer.Clock.LocalTime.Hour & ":" & My.Computer.Clock.LocalTime.Minute & ":" & My.Computer.Clock.LocalTime.Second & "Z</time>")
        ListBox1.Items.Add("<trk>")
        ListBox1.Items.Add("<name>NMEA Record GPX</name>")
        ListBox1.Items.Add("<trkseg>")
        Dim index1 = System.IO.File.ReadAllLines(fileo).Count()
        Dim lines() As String = IO.File.ReadAllLines(fileo)
        Dim ik = index1
        Dim i1
        For i1 = 1 To ik

            index1 = ik - i1

            p = index1 / ik * 100%

            ProgressBar1.Value = p





            Dim lineArr() As String = lines(index1).Split(",")


            If (lineArr(0).Contains("GPRMC")) Then
                Try
                    Dim dLat As Double = Convert.ToDouble(lineArr(3).Replace(".", ","))
                    dLat = dLat / 100
                    Dim lat() As String = dLat.ToString().Split(",")
                    Latitude = lat(0).ToString() + "." + ((Convert.ToInt32(lat(1)) / 60)).ToString("####")


                    Dim dLong As Double = Convert.ToDouble(lineArr(5).Replace(".", ","))
                    dLong = dLong / 100
                    Dim lon() As String = dLong.ToString().Split(",")
                    Longitude = lon(0).ToString() + "." + ((Convert.ToInt32(lon(1)) / 60)).ToString("####")



                Catch
                End Try
                Try


                    Dim time = lineArr(1)
                    time = Microsoft.VisualBasic.Left(time, 6)

                    Dim h = Microsoft.VisualBasic.Left(time, 2)


                    Dim min = Microsoft.VisualBasic.Left(time, 4)
                    min = Microsoft.VisualBasic.Right(min, 2)


                    Dim sec = Microsoft.VisualBasic.Right(time, 2)


                    Dim date1 = lineArr(9)
                    date1 = Microsoft.VisualBasic.Left(date1, 6)

                    Dim day = Microsoft.VisualBasic.Left(date1, 2)


                    Dim mon = Microsoft.VisualBasic.Left(date1, 4)
                    mon = Microsoft.VisualBasic.Right(mon, 2)


                    Dim j = Microsoft.VisualBasic.Right(date1, 2)




                Catch

                End Try




            End If


            If (lineArr(0).Contains("GPGGA")) Then




                Try
                    Dim m = lineArr(9)
                    Dim m2 = lineArr(11)

                    up = m2










                Catch

                End Try
            End If







            If Latitude = "" Then

            Else
                If Longitude = "" Then

                Else

                    If up = "" Then

                    Else


                        ListBox1.Items.Add("<trkpt lat= " & Chr(34) & "" & Latitude & "" & Chr(34) & "  lon= " & Chr(34) & "" & Longitude & "" & Chr(34) & " >")
                        ListBox1.Items.Add("<ele>" & up & "</ele>")
                        ListBox1.Items.Add("<time>" & My.Computer.Clock.LocalTime.Year & "-" & My.Computer.Clock.LocalTime.Month & "-" & My.Computer.Clock.LocalTime.Day & "T" & My.Computer.Clock.LocalTime.Hour & ":" & My.Computer.Clock.LocalTime.Minute & ":" & My.Computer.Clock.LocalTime.Second & "Z</time>")
                        ListBox1.Items.Add("</trkpt>")



                    End If

                End If
            End If












        Next









        ListBox1.Items.Add("</trkseg>")
        ListBox1.Items.Add("</trk>")
        ListBox1.Items.Add("</gpx>")


        Using strw As New IO.StreamWriter(New IO.FileStream(files, IO.FileMode.Create))
            For Each item As String In ListBox1.Items : strw.WriteLine(item) : Next : strw.Close()
        End Using

        sp2 = 0
        Label1.Text = "wait"
        Label1.ForeColor = Color.OrangeRed
        Label2.Text = "wait"
        Label2.ForeColor = Color.OrangeRed
        GroupBox1.Enabled = True
        GroupBox2.Enabled = True
        MsgBox("Ready")

    End Sub
    Dim up
    Dim Latitude
    Dim Longitude
    Dim lat
    Dim lon

    Private Sub Button3_Click(ByVal sEnder As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim sfd As New SaveFileDialog
        sfd.InitialDirectory = "C:\"
        sfd.Filter = "GPS-Datei (*.GPX)|*.gpx"
        sfd.Title = "Wählen Sie eine Datei zum Speichern aus"
        If sfd.ShowDialog() = DialogResult.OK Then

            files = sfd.FileName
            sp2 = sp2 + 1
            Label2.Text = "OK"
            Label2.ForeColor = Color.Green
            If sp2 = 2 Then
                Button1.Enabled = True
                
            End If
        Else
            MsgBox("Abbruch")
        End If
    End Sub

    Private Sub Button2_Click(ByVal sEnder As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sfd As New OpenFileDialog
        sfd.InitialDirectory = "C:\"
        sfd.Filter = "NMEA-LOG-Datei (*.LOG)|*.log"
        sfd.Title = "Wählen Sie eine Datei zum öffnen aus"
        If sfd.ShowDialog() = DialogResult.OK Then

            fileo = sfd.FileName
            sp2 = sp2 + 1
            Label1.Text = "OK"
            Label1.ForeColor = Color.Green
            If sp2 = 2 Then
                Button1.Enabled = True
               
            End If
        Else
            MsgBox("Abbruch")
        End If
    End Sub
    Dim sp2 = 0
End Class